// 引入 web3
import Web3 from 'web3'
export default () => {
    return new Promise((resolve, reject) => {
        var web3Provider;
        let web3js;
        if (window.ethereum) {
            web3Provider = window.ethereum;
            web3js = new Web3(web3Provider)
            try {
                let loadingInstance = loadMetamask.service({
                    text: "请注意查看钱包",
                });
                // 请求用户授权
                window.ethereum.send('eth_requestAccounts').then(() => {
                    loadingInstance.close();
                    resolve(web3js)
                }).catch(() => {
                    loadingInstance.close();
                    reject("User denied account access")
                });
            } catch (error) {
                // 用户不授权时
                console.log(error)
                reject("User denied account access")
            }

        } else {
            reject('未安装matemask')
        }


        // let web3js = new Web3(web3Provider); //web3js就是你需要的web3实例

        // web3js.eth.getAccounts(function (error, result) {
        //   if (!error)
        //     console.log(result) //授权成功后result能正常获取到账号了
        //     return result
        // });
    })
}